#define __ASM__
#include <arch/gdt.h>
#include <mm/memregion.h>

.section .bss
    .align 16
    kinit_stack_bottom:
        .skip 4095, 0
    kinit_stack_top:

.section .text
    .global hhk_entry
    hhk_entry:
        movl $kinit_stack_top, %esp

        subl $8, %esp
        call init_gdt

        # 加载 GDT
        movl $gdt, 2(%esp)
        movw gdt_limit, %ax
        movw %ax, (%esp)
        lgdt (%esp)
        addl $8,%esp

        movw $SS_R0_DATA, %ax
        movw %ax, %ds
        movw %ax, %es
        movw %ax, %fs
        movw %ax, %gs
        movw %ax, %ss

        // retf 会弹栈 8 byte, 如果 pushw 会多弹 2 byte 导致stackoverflow
        pushl $SS_R0_CODE
        pushl $after_gdt
        retf
    after_gdt:
        pushl %ebx
        call kernel_pre_init
        popl %ebx
        
        subl $8, %esp
        movl $idt, 2(%esp)
        movw idt_limit, %ax
        movw %ax, (%esp)
        lidt (%esp)
        addl $8,%esp

        movw $SS_TSS, %ax
        ltr %ax

        call build_process0
    spin:
        jmp spin